package nhatquang.gs.view;

import org.andengine.entity.Entity;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.util.modifier.IModifier;

import android.util.Log;

import nhatquang.gs.controler.ResourcesManager;
import nhatquang.gs.controler.ScenesManager;
import nhatquang.gs.model.BaseScene;
import nhatquang.gs.model.Public;
import nhatquang.gs.model.loadResourcesListener;

public class SplashScene extends BaseScene implements loadResourcesListener {
	private Sprite mSpriteLogo;
	private Entity mEntitySpalsh;
	private SequenceEntityModifier entityModifier;
	private boolean isRun;

	@Override
	public void createScene() {
		mCamera.setCenter(Public.WIDTH_SCREEN / 2, Public.HEIGHT_SCREEN / 2);
		this.setBackground(new Background(0, 0, 0));
		mEntitySpalsh = new Entity();
		draw();
		ResourcesManager.getInstance().setloadResourcesListener(this);
		mEngine.registerUpdateHandler(new FPSLogger());
	}

	public void draw() {
		mSpriteLogo = new Sprite(Public.WIDTH_SCREEN / 2
				- mResourcesManager.mTextureRegionSplash.getWidth() / 2,
				Public.HEIGHT_SCREEN / 2
						- mResourcesManager.mTextureRegionSplash.getHeight()
						/ 2, mResourcesManager.mTextureRegionSplash,
				mResourcesManager.VBOM);
		mSpriteLogo.registerEntityModifier(new AlphaModifier(5, 0, 1f));
		mEntitySpalsh.attachChild(mSpriteLogo);
		this.attachChild(mEntitySpalsh);
	}

	@Override
	public void onBackKeyPressed() {

	}

	@Override
	public void RunApp() {
		if (!isRun) {
			Log.d("nhat quang",
					"da chay vao run app==================================");
			ScenesManager.getInstance().createMenu();
		}
		isRun = true;
	}

	public void entityModiferLogoSprite(Sprite entity) {
		entityModifier = new SequenceEntityModifier(
				new IEntityModifierListener() {
					@Override
					public void onModifierStarted(
							final IModifier<IEntity> pModifier,
							final IEntity pItem) {
						// mResourcesManager.loadResource();
					}

					@Override
					public void onModifierFinished(
							final IModifier<IEntity> pEntityModifier,
							final IEntity pEntity) {
						try {
							// ScenesManager.getInstance().createMenu();
						} catch (Exception e) {

						}
					}
				}, new AlphaModifier(5, 0, 1f));
		entity.registerEntityModifier(entityModifier);
	}

}
